package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.algorithm.encrypt.symmetric.AES;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.reflection.ClassConvertUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.ts.api.http.org.entity.apply.OrgApplyKaiDanYinXiong;
import com.ts.api.http.org.entity.apply.OrgApplyKaiDanYinXiongData;
import com.ts.api.http.org.entity.dataexchange.OrgDataExchangeKaiDanYinXiong;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;

/**
 * 开单英雄
 *
 * @author WJB
 */
@ApiPlan(productId = "276", type = ApiPlanConvertType.ORG_PHONE_MD5)
public class ApiOrgPhoneMd5KaiDanYinXiong implements ApiOrgPhoneMd5ConvertHandler {

    private static final String KEY = "kGdzJZp9OHXaXoQe";
    private static final String CHANNEL_ID = "FC56DB";

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) throws Exception {
        OrgDataExchangeKaiDanYinXiong orgDataExchangeKaiDanYinXiong = new OrgDataExchangeKaiDanYinXiong();
        orgDataExchangeKaiDanYinXiong.setMobileMd5(apiDataExchangeParameter.getPhoneMd5());
        orgDataExchangeKaiDanYinXiong.setChannelId(CHANNEL_ID);

        String existLocal = apiDataExchangeParameter.getExistLocal();
        if (ClassConvertUtils.toBoolean(existLocal)) {
            orgDataExchangeKaiDanYinXiong.setCustomerStatus("2");
        } else {
            orgDataExchangeKaiDanYinXiong.setCustomerStatus("1");
        }
        return JsonUtils.toJson(orgDataExchangeKaiDanYinXiong);
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) throws Exception {
        //  拓展判断信息
        ApiFieldConverter apiFieldConverter = ApiFieldConverter.create(apiApplyParameter);

        OrgApplyKaiDanYinXiong orgApplyKaiDanYinXiong = new OrgApplyKaiDanYinXiong();
        orgApplyKaiDanYinXiong.setUpChannelId("F8809A");

        OrgApplyKaiDanYinXiongData orgApplyKaiDanYinXiongData = new OrgApplyKaiDanYinXiongData();
        orgApplyKaiDanYinXiongData.setName(apiApplyParameter.getUserName());
        orgApplyKaiDanYinXiongData.setMobile(apiApplyParameter.getPhone());
        orgApplyKaiDanYinXiongData.setAreaId(ClassConvertUtils.toInt(apiApplyParameter.getAreaCode()));

        //  年龄
        orgApplyKaiDanYinXiongData.setAge(apiFieldConverter.getAgeInt());

        //  学历
        if (ValidParam.isNotEmpty(apiApplyParameter.getEducationAttainment())) {
            Integer text = ChooseEq.create()
                .when(apiApplyParameter.getEducationAttainment(), "1", 1)
                .when(apiApplyParameter.getEducationAttainment(), "2", 3)
                .when(apiApplyParameter.getEducationAttainment(), "3", 2)
                .when(apiApplyParameter.getEducationAttainment(), "4", 4)
                .when(apiApplyParameter.getEducationAttainment(), "5", 5)
                .when(apiApplyParameter.getEducationAttainment(), "6", 6)
            .end(7);
            orgApplyKaiDanYinXiongData.setEducational(text);
        }

        //  逾期
        if (ValidParam.isNotEmpty(apiApplyParameter.getOverdue())) {
            Integer text = ChooseEq.create()
                .when(apiApplyParameter.getOverdue(), "1", 2)
                .when(apiApplyParameter.getOverdue(), "2", 1)
                .when(apiApplyParameter.getOverdue(), "3", 1)
            .end();
            orgApplyKaiDanYinXiongData.setOverdue(text);
        }

        //  性别
        Integer sex = ChooseEq.create()
            .when(apiApplyParameter.getSex(), "0", 1)
            .when(apiApplyParameter.getSex(), "1", 2)
        .end();
        orgApplyKaiDanYinXiongData.setGender(sex);

        //  房产
        if (ValidParam.isNotEmpty(apiApplyParameter.getRealEstateInfo())) {
            Integer house = ChooseEq.create()
                    .when(apiApplyParameter.getRealEstateInfo(), "1", 3)
                    .when(apiApplyParameter.getRealEstateInfo(), "2", 2)
                    .when(apiApplyParameter.getRealEstateInfo(), "3", 1)
                    .end();
            orgApplyKaiDanYinXiongData.setHouse(house);
        }
        //  芝麻分
        if (ValidParam.isNotEmpty(apiApplyParameter.getProvidentFund())) {
            Integer zhima = ChooseEq.create()
                    .when(apiApplyParameter.getSesameCredit(), "1", 1)
                    .when(apiApplyParameter.getSesameCredit(), "2", 2)
                    .when(apiApplyParameter.getSesameCredit(), "3", 3)
                    .when(apiApplyParameter.getSesameCredit(), "4", 4)
                    .end();
            orgApplyKaiDanYinXiongData.setSesame(zhima);
        }
        //  贷款额度
        orgApplyKaiDanYinXiongData.setLoanQuota(apiFieldConverter.getLoanAmountInt());
        //  公积金
        if (ValidParam.isNotEmpty(apiApplyParameter.getProvidentFund())) {
            Integer text = ChooseEq.create()
                    .when(apiApplyParameter.getProvidentFund(), "1", 3)
                    .when(apiApplyParameter.getProvidentFund(), "2", 2)
                    .when(apiApplyParameter.getProvidentFund(), "3", 1)
                    .end();
            orgApplyKaiDanYinXiongData.setAccumulationFund(text);
        }
        //  信用卡额度
        if (ValidParam.isNotEmpty(apiApplyParameter.getCreditLimit())) {
            Integer text = ChooseEq.create()
                    .when(apiApplyParameter.getCreditLimit(), "1", 0)
                    .when(apiApplyParameter.getCreditLimit(), "2", 3000)
                    .when(apiApplyParameter.getCreditLimit(), "3", 10000)
                    .when(apiApplyParameter.getCreditLimit(), "4", 30000)
                    .when(apiApplyParameter.getCreditLimit(), "5", 50000)
                    .when(apiApplyParameter.getCreditLimit(), "6", 3000)
                    .end();
            orgApplyKaiDanYinXiongData.setCreditCardQuota(text);
        }
        //  花呗额度
        if (ValidParam.isNotEmpty(apiApplyParameter.getHbLimit())) {
            Integer huaBei = ChooseEq.create()
                    .when(apiApplyParameter.getHbLimit(), "1", 0)
                    .when(apiApplyParameter.getHbLimit(), "2", 3000)
                    .when(apiApplyParameter.getHbLimit(), "3", 10000)
                    .when(apiApplyParameter.getHbLimit(), "4", 30000)
                    .when(apiApplyParameter.getHbLimit(), "5", 50000)
                    .end(0);
            orgApplyKaiDanYinXiongData.setHuabeiQuota(huaBei);
        }
        //  职业
        orgApplyKaiDanYinXiongData.setWork(apiFieldConverter.getCareerText());
        //  社保
        if (ValidParam.isNotEmpty(apiApplyParameter.getSocialSecurity())) {
            Integer text = ChooseEq.create()
                    .when(apiApplyParameter.getSocialSecurity(), "1", 3)
                    .when(apiApplyParameter.getSocialSecurity(), "2", 2)
                    .when(apiApplyParameter.getSocialSecurity(), "3", 1)
                    .end();
            orgApplyKaiDanYinXiongData.setSocialInsurance(text);
        }
        //  车产
        if (ValidParam.isNotEmpty(apiApplyParameter.getCarInfo())) {
            Integer car = ChooseEq.create()
                    .when(apiApplyParameter.getCarInfo(), "1", 3)
                    .when(apiApplyParameter.getCarInfo(), "2", 2)
                    .when(apiApplyParameter.getCarInfo(), "3", 1)
                    .end();
            orgApplyKaiDanYinXiongData.setCarStatus(car);
        }
        //  保单
        if (ValidParam.isNotEmpty(apiApplyParameter.getInsurancePolicy())) {
            Integer text = ChooseEq.create()
                    .when(apiApplyParameter.getInsurancePolicy(), "1", 3)
                    .when(apiApplyParameter.getInsurancePolicy(), "2", 2)
                    .when(apiApplyParameter.getInsurancePolicy(), "3", 1)
                    .end();
            orgApplyKaiDanYinXiongData.setPolicy(text);
        }
        //  白条额度
        if (ValidParam.isNotEmpty(apiApplyParameter.getBtLimit())) {
            Integer baiTiao = ChooseEq.create()
                    .when(apiApplyParameter.getBtLimit(), "1", 0)
                    .when(apiApplyParameter.getBtLimit(), "2", 3000)
                    .when(apiApplyParameter.getBtLimit(), "3", 10000)
                    .when(apiApplyParameter.getBtLimit(), "4", 30000)
                    .when(apiApplyParameter.getBtLimit(), "5", 50000)
                    .end(0);
            orgApplyKaiDanYinXiongData.setBaitiaoQuota(baiTiao);
        }
        //   月收入
        orgApplyKaiDanYinXiongData.setIncome(apiFieldConverter.getMonthlySalaryNumber());
        // 营业执照
        if (ValidParam.isNotEmpty(apiApplyParameter.getBizLicenseYear())) {
            Integer text = ChooseEq.create()
                    .when(apiApplyParameter.getBizLicenseYear(), "1", 2)
                    .end(1);
            orgApplyKaiDanYinXiongData.setBusiness(text);
        }

        //  新老用户
        String existLocal = apiApplyParameter.getExistLocal();
        if (ClassConvertUtils.toBoolean(existLocal)) {
            orgApplyKaiDanYinXiongData.setCustomerStatus(2);
        } else {
            orgApplyKaiDanYinXiongData.setCustomerStatus(1);
        }

        //  渠道id
        orgApplyKaiDanYinXiongData.setChannelId(apiApplyParameter.getApiClientId());

        //  加密数据
        AES cbc = AES.createEcb(KEY.getBytes());
        String encrypt = cbc.encryptToBase64Str(JsonUtils.toJson(orgApplyKaiDanYinXiongData));
        orgApplyKaiDanYinXiong.setEncryptStr(encrypt);

        return JsonUtils.toJson(orgApplyKaiDanYinXiong);
    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
